
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsNewDownload;

public class GenericDownloadNewDownloadDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadNewDownloadDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadNewDownloadModel#retrieveByPK(int)
	 */
	public DownloadsNewDownload retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadNewDownloadModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		DownloadsNewDownload s = new DownloadsNewDownload();
		
		if (rs.next()) {
			fillDownNewDownFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}
	public void deleteByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadNewDownloadModel.deleteByID"));
		p.setInt(1, id);
		
		p.executeUpdate();
		p.close();
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillDownNewDownFromResultSet(DownloadsNewDownload s, ResultSet rs)throws Exception {
		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setFILESIZE(rs.getInt("FILESIZE"));
		s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setNAME(rs.getString("NAME"));
		s.setSID(rs.getInt("SID"));
		s.setSUBMITTER(rs.getString("SUBMITTER"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		s.setVERSION(rs.getString("VERSION"));
		
	}

	public List selectAll() throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadNewDownloadModel.selectAll"));
			
			
		
		
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			DownloadsNewDownload u = new DownloadsNewDownload();
			fillDownNewDownFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadNewDownloadModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadNewDownloadModel.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}
	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadNewDownloadModel#addNew(org.jetnuke.entities.DownloadsNewDownload)
	 */
	public int addNew(DownloadsNewDownload dd) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("DownloadNewDownloadModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("DownloadNewDownloadModel.lastGeneratedDownloadNewID"));
		initNewDownload(dd,p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}
	/**
	 * @param dd
	 * @param p
	 */
	private void initNewDownload(DownloadsNewDownload dd, PreparedStatement p)throws Exception {
		p.setInt(1,dd.getCID());
		p.setString(2,dd.getDESCRIPTION());
		p.setString(3,dd.getEMAIL());
		p.setInt(4,dd.getFILESIZE());
		p.setString(5,dd.getHOMEPAGE());
		p.setString(6,dd.getNAME());
		p.setInt(7,dd.getSID());
		p.setString(8,dd.getSUBMITTER());
		p.setString(9,dd.getTITLE());
		p.setString(10,dd.getURL());
		p.setString(11,dd.getVERSION());
		
	}

}
